package org.liang.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.liang.base.annotation.RonghuaPermission;
import org.liang.base.utils.AjaxResult;
import org.liang.base.utils.PageInfo;
import org.liang.system.domain.Employee;
import org.liang.system.query.DepartmentQuery;
import org.liang.system.query.EmployeeQuery;
import org.liang.system.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
@Api(value = "员工管理接口",description = "员工管理")
@RonghuaPermission(name = "员工管理",desc = "员工相关接口")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    //新增或者修改员工
    @ApiOperation(value = "新增或者修改员工")
    @PutMapping
    @RonghuaPermission(name = "员工保存",desc = "员工新增和修改")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if(Objects.isNull(employee.getId())){
                employeeService.insert(employee);
                return AjaxResult.success(employee);
            }else {
                employeeService.update(employee);
                return AjaxResult.success(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //删除员工
    @ApiOperation(value = "删除员工")
    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "员工删除",desc = "删除指定员工")
    public AjaxResult delete(@PathVariable Serializable id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //批量删除员工
    @ApiOperation(value = "批量删除员工")
    @PatchMapping
    @RonghuaPermission(name = "员工删除",desc = "批量删除员工")
    public AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //查询指定id的员工
    @ApiOperation(value = "查询指定id的员工")
    @GetMapping("/{id}")
    @RonghuaPermission(name = "员工查询",desc = "查询指定员工")
    public AjaxResult selectById(@PathVariable Serializable id){
        try {
            Employee employee = employeeService.selectById(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //查询所有的员工
    @ApiOperation(value = "查询所有的员工")
    @GetMapping
    @RonghuaPermission(name = "员工查询",desc = "查询所有员工")
    public AjaxResult selectAll(){
        try {
            List<Employee> employees = employeeService.selectAll();
            return AjaxResult.success(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //分页查询
    @PostMapping
    @RonghuaPermission(name = "员工分页",desc = "分页查询员工")
    public AjaxResult queryPage(@RequestBody EmployeeQuery employeeQuery){
        try {
            PageInfo pageInfo = employeeService.queryPage(employeeQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
